package com.soft.idc.module.nettopo;

import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import com.soft.idc.common.model.IdcOrg;
import com.soft.idc.common.util.JQList;
import com.soft.idc.common.util.JQPage;

public class NetTopoController extends Controller {

	public void index() {
		this.renderJsp("/nettopo/nettopo.jsp");
	}

	public void loadNettopo() {
		IdcOrg org = (IdcOrg) this.getSessionAttr("SYS_USER_ORG");
		String sql = "select id,text,x,y,width,height,url,node_type  from idc_net_topo where org_id = ?";
		List<Record> nodeList = Db.find(sql, org.getOrgId());

		String sqlLink = "select text,color,link_type,(select id from idc_net_topo where id = l.aNode_id) as aNode,(select id from idc_net_topo where id = l.zNode_id) as zNode from idc_link_topo l where type = ? and org_id = ?";
		List<Record> linkList = Db.find(sqlLink, "net_topo", org.getOrgId());

		this.setAttr("net_topo_node", nodeList);
		this.setAttr("net_topo_link", linkList);
		this.renderJson();
	}

	public void loadLink() {
		IdcOrg org = (IdcOrg) this.getSessionAttr("SYS_USER_ORG");
		String sql = "select l.* from idc_link l join idc_link_orgrel o on l.link_id = o.link_id"
				+ " where o.org_id = ?";
		List<Record> link = Db.find(sql, org.getOrgId());
		JQList list = new JQList(link);
		this.renderJson(list);
	}

	public void clearCache() {
		CacheKit.remove("MainPageCache", "net_topo_node");
		CacheKit.remove("MainPageCache", "net_topo_link");
		this.renderJson("result", "success");
	}
}
